package top.oylan.mp.service;

import org.springframework.stereotype.Service;
import top.oylan.mp.entity.Menu;
import top.oylan.mp.entity.example.MenuExample;
import top.oylan.mp.entity.vo.MenuVo;
import top.oylan.mp.mapper.MenuMapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MenuService {

    @Resource
    MenuMapper menuMapper;

    public List<Menu> selectAll() {
        MenuExample example = new MenuExample();
        return menuMapper.selectByExample(example);
    }

    public void update(Menu menu) {
        if (menu.getParentId() > 0) {
            Menu parent = menuMapper.selectByPrimaryKey(menu.getParentId());
            menu.setLevel((byte) (parent.getLevel() + 1));
        }
        menuMapper.updateByPrimaryKeySelective(menu);
    }

    public void add(Menu menu) {
        menu.setCreateTime(new Date());
        if (menu.getParentId() > 0) {
            Menu parent = menuMapper.selectByPrimaryKey(menu.getParentId());
            menu.setLevel((byte) (parent.getLevel() + 1));
        }
        menuMapper.insertSelective(menu);
    }

    public void delete(Integer id) {
        menuMapper.deleteByPrimaryKey(id);
    }

    public List<MenuVo> selectAllMenus() {
        return menuMapper.selectAllMenus();
    }
}
